package com.cjl.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjl.auth.mapper.SysPostMapper;
import com.cjl.auth.mapper.SysUserMapper;
import com.cjl.auth.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjl.model.system.*;
import com.cjl.security.custom.LoginUserInfoHelper;
import com.cjl.vo.system.SysUserQueryVo;
import org.apache.fop.layoutmgr.PageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author cjl
 * @since 2023-03-14
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysPostService sysPostService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public void updateStatus(Long id, Integer status) {
        baseMapper.updateStatus(id, status);
    }

    @Override
    public SysUser getUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        SysUser sysUser = baseMapper.selectOne(wrapper);
        return sysUser;
    }

    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = baseMapper.selectById(LoginUserInfoHelper.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("name", sysUser.getName());
        map.put("phone", sysUser.getPhone());
        return map;
    }

    @Override
    public IPage<SysUser> queryUser(SysUserQueryVo sysUserQueryVo, Page<SysUser> pageParam) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        if(!StringUtils.isEmpty(username)){
            wrapper.like(SysUser::getUsername, username);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            wrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            wrapper.le(SysUser::getCreateTime, createTimeEnd);
        }
        Page<SysUser> sysUserPage = baseMapper.selectPage(pageParam, wrapper);
        List<SysUser> records = sysUserPage.getRecords();
        for (SysUser sysUser : records) {
            LambdaQueryWrapper<SysUserRole> userRoleWrapper = new LambdaQueryWrapper<>();
            userRoleWrapper.eq(SysUserRole::getUserId, sysUser.getId());
            List<SysUserRole> sysUserRoles = sysUserRoleService.list(userRoleWrapper);
            List<SysRole> sysRoleList = new ArrayList<>();
            for (SysUserRole sysUserRole : sysUserRoles) {
                SysRole sysRole = sysRoleService.getById(sysUserRole.getRoleId());
                sysRoleList.add(sysRole);
            }
            sysUser.setRoleList(sysRoleList);

            SysPost sysPost = sysPostService.getById(sysUser.getPostId());
            if(sysPost != null){
                sysUser.setPostName(sysPost.getName());
            }

            SysDept sysDept = sysDeptService.getById(sysUser.getDeptId());
            if(sysDept != null){
                sysUser.setDeptName(sysDept.getName());
            }
        }
        return sysUserPage;
    }

}
